<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="fns" uri="/WEB-INF/tlds/fns.tld" %>
<%@ taglib prefix="shiro" uri="/WEB-INF/tlds/shiros.tld" %>
<c:set var="path" value="${pageContext.request.contextPath}"/>
<!-- Content Header (Page header) -->
<section class="content-header" style="">
    <h1>
        档案库管理
        <small>档案库管理页面</small>
    </h1>
    <ol class="breadcrumb">
        <li><a href="#"><i class="fa fa-dashboard"></i> 主页</a></li>
        <li><a href="#">业务管理</a></li>
        <li class="active">档案库</li>
    </ol>
</section>

<section class="content">
    <div class="row">
        <div class="col-xs-12">
            <div class="box">
                <div class="box-body">
                    <table id="table"></table>
                </div>
            </div>
        </div>

    </div>
</section>

<!-- add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel"
        >
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="addModalLabel">添加担保信息</h4>
            </div>
            <div class="modal-body">
                <form id="addForm" enctype="multipart/form-data">
                    <input type="hidden" id="compay_id" name="id" readonly required>

                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>名称</label>
                        <input id="com_name" type="text" class="form-control" name="comName" placeholder="担保人名称"
                               minlength="1" required>
                    </div>
                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>类别:</label>
                        <select name="comType" id="comType" class="form-control chosen-select"
                                data-placeholder="请选择类别" onchange="comTypeChange()" required>
                            <option value="4">企业担保</option>
                            <option value="5">个人担保</option>
                        </select>

                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="form-group">
                        <label class="control-label">担保金额</label>
                        <input id="credit_Line" type="number" class="form-control" name="creditLine"
                               placeholder="输入担保金额"
                               required>

                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="box box-danger">
                        <div class="box-header with-border">
                            <h3 class="box-title">初始资料上传</h3>
                        </div>
                        <div class="box-body">
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id1" class="col-sm-6 control-label" id="input-idP1Label"
                                           style="display:none;">身份证复印件:</label>
                                    <label for="input-id1" class="col-sm-6 control-label" id="input-idC1Label">企业营业执照（副本）、开户许可证、机构信用代码证:</label>
                                    <input id="input-id1" name="input-id1" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id2" class="col-sm-6 control-label" id="input-idP2Label"
                                           style="display:none;">户口本:</label>
                                    <label for="input-id2" class="col-sm-6 control-label"
                                           id="input-idC2Label">法人身份证:</label>
                                    <input id="input-id2" name="input-id2" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id3" class="col-sm-6 control-label" id="input-idP3Label"
                                           style="display:none;">收入证明:</label>
                                    <label for="input-id3" class="col-sm-6 control-label" id="input-idC3Label">法人及企业的人行征信:</label>
                                    <input id="input-id3" name="input-id3" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id4" class="col-sm-6 control-label" id="input-idP4Label"
                                           style="display:none;">最新的个人征信:</label>
                                    <label for="input-id4" class="col-sm-6 control-label" id="input-idC4Label">验资报告、公司章程以及工商局出具的变更证明:</label>
                                    <input id="input-id4" name="input-id4" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id5" class="col-sm-6 control-label" id="input-idP5Label"
                                           style="display:none;">结婚证或单身证明:</label>
                                    <label for="input-id5" class="col-sm-6 control-label" id="input-idC5Label">近三年的审计报告、本年当期的财务报表:</label>
                                    <input id="input-id5" name="input-id5" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id6" class="col-sm-6 control-label"
                                           id="input-idC6Label">增值税纳税申报表:</label>
                                    <input id="input-id6" name="input-id6" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id7" class="col-sm-6 control-label" id="input-idC7Label">法人夫妻双方的结婚证或户口本:</label>
                                    <input id="input-id7" name="input-id7" type="file" data-show-caption="true">
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id8" class="col-sm-6 control-label" id="input-idP8Label"
                                           style="display:none;">近半年银行卡流水清单:</label>
                                    <input id="input-id8" name="input-id8" type="file" data-show-caption="true"
                                           style="display:none;">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" form="addForm" class="btn btn-primary" id="submitImgAdd">
                    确定
                </button>
            </div>
            <div id="J_progress_label" style="display: none;position: absolute;width: 200px;height: 60px;text-align: center;line-height: 60px;top: 0;left: 0;right: 0;bottom: 0;border-radius:15px;margin: auto;box-shadow:0px 0px 5px #e5f9f2;background:#f63;z-index:999;color:white;">

            </div>
        </div>
    </div>
</div>

<!-- edit Modal -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="editModalLabel">修改档案信息</h4>
            </div>
            <div class="modal-body">
                <form id="editForm">
                    <input type="hidden" name="id"/>

                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>企业名称:</label>
                        <input id="comName" type="text" class="form-control" name="comName"
                               minlength="1" required>

                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="form-group">
                        <label class="control-label"><span class="asterisk">*</span>营业执照:</label>
                        <input id="businessCode" type="text" class="form-control" name="businessCode"
                               placeholder="businessCode"
                               minlength="1" required>

                        <div class="help-block with-errors"></div>
                    </div>
                    <div class="form-group">
                        <label class="control-label">授信额度:</label>
                        <input id="creditLine" type="number" class="form-control" name="creditLine" placeholder="授信额度"
                               required>

                        <div class="help-block with-errors"></div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="submit" form="editForm" class="btn btn-primary"
                        data-action="{type:'submit',form:'#editForm',url:'<%=request.getContextPath()%>/company/editCompanyInfo',after:'$.myAction.refreshTable'}">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>

<!-- delete modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="deleteSmallModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="deleteSmallModalLabel">设置为休眠户</h4>
            </div>
            <div class="modal-body">
                确定要休眠企业名称为 <span class="records"></span> 的档案信息吗?

                <form id="deleteForm">
                    <div class="form-group">
                        <label class="control-label" for="comName">原因:</label>
                        <textarea autofocus class="form-control" name="comName" minlength="5"
                                  maxlength="300" placeholder="输入原因" required>
                        </textarea>

                        <div class="help-block with-errors"></div>
                    </div>
                </form>

            </div>

            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" form="deleteForm" class="btn btn-primary"
                        data-action="{type:'submit',form:'#deleteForm',url:'<%=request.getContextPath()%>/archives/delete',after:'$.myAction.refreshTable'}">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>

<%--bestReport modal--%>
<div class="modal fade" id="bestReportModal" tabindex="-1" role="dialog" aria-labelledby="bestReportLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="bestReportModalLabel">尽调信息上传</h4>
            </div>
            <div class="modal-body">
                <form id="bestReportForm" enctype="multipart/form-data">
                    <input type="hidden" id="bestReport_compay_id" name="id" readonly required>
                    <input type="hidden" id="no_status" name="operateType" value="11" readonly required>

                    <div class="box box-danger">
                        <div class="box-header with-border">
                            <h3 class="box-title">尽调资料上传</h3>
                        </div>
                        <div class="box-body">
                            <div class="row">
                                <div class="form-group">
                                    <label for="input-id21" class="col-sm-6 control-label"
                                           id="input-idC1Labe2l">尽调报告资料:</label>
                                    <input id="input-id21" name="input-id21" type="file" data-show-caption="true">
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" id="submitImgAdd1">
                    确定
                </button>
            </div>
            <div id="J_progress_label" style="display: none;position: absolute;width: 200px;height: 60px;text-align: center;line-height: 60px;top: 0;left: 0;right: 0;bottom: 0;border-radius:15px;margin: auto;box-shadow:0px 0px 5px #e5f9f2;background:#f63;z-index:999;color:white;">

            </div>
        </div>
    </div>
</div>

<!-- cancelStorage modal -->
<div class="modal fade" id="cancelStorageModal" tabindex="-1" role="dialog" aria-labelledby="cancelStorageModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="cancelStorageModalLabel">存档设置</h4>
            </div>
            <div class="modal-body">
                <form id="cancelStorageForm"></form>
                确定要企业名为 <span class="records"></span> 的档案取消存档吗?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" form="cancelStorageForm" class="btn btn-primary"
                        data-action="{type:'submit',form:'#cancelStorageForm',url:'<%=request.getContextPath()%>/archives/storage',after:'$.myAction.refreshTable'}">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>

<!-- storage modal -->
<div class="modal fade" id="storageModal" tabindex="-1" role="dialog" aria-labelledby="storageModalLabel">
    <div class="modal-dialog modal-sm" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="storageModalLabel">存档设置</h4>
            </div>
            <div class="modal-body">
                <form id="storageForm"></form>
                确定要企业名为 <span class="records"></span> 的档案取消存档吗?
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" form="storageForm" class="btn btn-primary"
                        data-action="{type:'submit',form:'#deleteForm',url:'<%=request.getContextPath()%>/archives/checkStatus',after:'$.myAction.refreshTable'}">
                    确定
                </button>
            </div>
        </div>
    </div>
</div>


<script>
    //Date picker
    $('#datepicker').datepicker({
        autoclose: true
    })

    /*function loadHref(s) {
     $.myAdmin.loadContent(s);
     }*/

    $("#submitImgAdd").click(function () {

        //var id = $("#id").val();
        var formData = new FormData();
        var sizeTotal=0;
        var sizeFlag=true;
        $("#addForm :input").each(function () {

            var type = $(this).attr("type");
            var name = $(this).attr("id");
            if (type == "file") {
                var label_name = $('label[for="' + name + '"]').text();
                formData.append(label_name, $(this)[0].files[0]);
                sizeTotal=sizeTotal+$(this)[0].files[0].size;
            }
        });

        if(sizeTotal>200*1024*1024){
            sizeFlag=false;
            alert("文件需小于200M");
            return;
        }

        if(sizeFlag) {
            var that = this;
            var loadnum = 0;
            var oldloaded = 0;

            $.ajax({
                type: 'post',
                url: '/company/saveSupportCompanyInfo?' + $('#addForm').serialize(),
                cache: false,
                data: formData,
                contentType: false,
                processData: false,
                xhr: function () {
                    myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) {
                        $(that).parents('.modal ').find('#J_progress_label').show();
                        myXhr.upload.addEventListener('progress', function (e) {
                            if (e.lengthComputable) {
                                if (e.loaded != 0) {
                                    loadnum += Math.abs(e.loaded - oldloaded);
                                    console.log(e.loaded, loadnum, e.total);
                                    oldloaded = e.loaded;
                                } else {
                                    oldloaded = 0;
                                }

                                var percent = Math.floor(loadnum / e.total * 100);
                                if (percent <= 100) {
                                    $(that).parents('.modal ').find("#J_progress_label").html('已上传：' + percent + '%');
                                }
                                if (percent >= 100) {
                                    $(that).parents('.modal ').find("#J_progress_label").html('文件上传完毕，请等待处理...');
                                }
                            }
                        }, false);
                    }
                    return myXhr;
                },
                success: function (data) {
                    alert('上传图片成功');
                    $("#submitImgAdd").attr("disabled", "disabled");
                },
                error: function () {
                    alert("上传图片失败");
                }
            })
        }
    })


    $("#submitImgAdd1").click(function () {

        var id = $("#bestReport_compay_id").val();
        var formData = new FormData();
        var sizeTotal=0;
        var sizeFlag=true;
        $("#bestReportForm :input").each(function () {

            var type = $(this).attr("type");
            var name = $(this).attr("id");
            if (type == "file") {
                var label_name = $('label[for="' + name + '"]').text();
                formData.append(label_name,$(this)[0].files[0]);
                sizeTotal=sizeTotal+$(this)[0].files[0].size;
            }
        });
        if(sizeTotal>200*1024*1024){
            sizeFlag=false;
            alert("文件需小于200M");
            return;
        }
        if(sizeFlag) {
            var that = this;
            var loadnum = 0;
            var oldloaded = 0;
            $.ajax({
                type: 'post',
                url: '/company/saveCompanyUploadInfo?id=' + id + '&operateType=11',
                cache: false,
                data: formData,
                contentType: false,
                processData: false,
                xhr: function () {
                    myXhr = $.ajaxSettings.xhr();
                    if (myXhr.upload) {
                        $(that).parents('.modal ').find('#J_progress_label').show();
                        myXhr.upload.addEventListener('progress', function (e) {
                            if (e.lengthComputable) {
                                if (e.loaded != 0) {
                                    loadnum += Math.abs(e.loaded - oldloaded);
                                    console.log(e.loaded, loadnum, e.total);
                                    oldloaded = e.loaded;
                                } else {
                                    oldloaded = 0;
                                }

                                var percent = Math.floor(loadnum / e.total * 100);
                                if (percent <= 100) {
                                    $(that).parents('.modal ').find("#J_progress_label").html('已上传：' + percent + '%');
                                }
                                if (percent >= 100) {
                                    $(that).parents('.modal ').find("#J_progress_label").html('文件上传完毕，请等待处理...');
                                }
                            }
                        }, false);
                    }
                    return myXhr;
                },
                success: function (data) {
                    alert('上传图片成功');
                    $("#submitImgAdd").attr("disabled", "disabled");
                },
                error: function () {
                    alert("上传图片失败");
                }
            })
        }
    })


    function comTypeChange() {
        var objS = document.getElementById("comType");
        var selectI = objS.options[objS.selectedIndex].value;
        if (selectI == "4") {
            $("#input-idP1Label").attr("style", "display:none");
            $("#input-idP2Label").attr("style", "display:none");
            $("#input-idP3Label").attr("style", "display:none");
            $("#input-idP4Label").attr("style", "display:none");
            $("#input-idP5Label").attr("style", "display:none");
            $("#input-idP8Label").attr("style", "display:none");
            $("#input-id8").attr("style", "display:none");

            $("#input-idC1Label").attr("style", "display:block");
            $("#input-idC2Label").attr("style", "display:block");
            $("#input-idC3Label").attr("style", "display:block");
            $("#input-idC4Label").attr("style", "display:block");
            $("#input-idC5Label").attr("style", "display:block");
            $("#input-idC6Label").attr("style", "display:block");
            $("#input-id6").attr("style", "display:block");
            $("#input-idC7Label").attr("style", "display:block");
            $("#input-id7").attr("style", "display:block");

        } else {
            $("#input-idC1Label").attr("style", "display:none");
            $("#input-idC2Label").attr("style", "display:none");
            $("#input-idC3Label").attr("style", "display:none");
            $("#input-idC4Label").attr("style", "display:none");
            $("#input-idC5Label").attr("style", "display:none");
            $("#input-idC6Label").attr("style", "display:none");
            $("#input-id6").attr("style", "display:none");
            $("#input-idC7Label").attr("style", "display:none");
            $("#input-id7").attr("style", "display:none");

            $("#input-idP1Label").attr("style", "display:block");
            $("#input-idP2Label").attr("style", "display:block");
            $("#input-idP3Label").attr("style", "display:block");
            $("#input-idP4Label").attr("style", "display:block");
            $("#input-idP5Label").attr("style", "display:block");
            $("#input-idP8Label").attr("style", "display:block");
            $("#input-id8").attr("style", "display:block");
        }
    }

    var $table = $('#table');

    $(function () {
        // bootstrap table初始化
        // http://bootstrap-table.wenzhixin.net.cn/zh-cn/documentation/
        $table.bootstrapTable({
            url: path + '/archives/list',
            idField: 'id',
            searchOnEnterKey: true,
            showColumns: false,
            detailView: false,
            columns: [
                {field: 'id', title: '编号'},
                {field: 'comName', title: '企业名称',
                    formatter: function (value, row, index) {
                        return [
                            '<a href="#archives/detail?id=' + row.id + '">'+value+'</a>'
                        ]
                    }
                },
                {field: 'businessCode', title: '营业执照编号（身份证号）'},
                /*{field: 'creditLine', title: '授信额度'},*/
                {
                    field: 'comType', title: '类别', formatter: function (value, row, index) {
                    if (value == 1) {
                        return "核心企业"
                    } else if (value == 2) {
                        return "一般企业"
                    } else if (value == 3) {
                        return "个人"
                    }
                },
                },
                {
                    field: 'status', title: '状态', formatter: function (value, row, index) {
                    if (value == 1) {
                        return "待尽调"
                    } else if (value == 2) {
                        return "待部门审核"
                    } else if (value == 3) {
                        return "待风控审核"
                    } else if (value == 4) {
                        return "待上线"
                    } else if (value == 5) {
                        return "贷后管理"
                    } else if (value == 6) {
                        return "存档"
                    } else if (value == 7) {
                        return "休眠"
                    }
                },
                },
                {field: 'inputTime', title: '录入时间'},
                {field: 'activeTime', title: '激活时间'},
                {field: 'upTime', title: '上线时间'},
                /*{field: 'supportId', title: '担保方信息'},*/
                {
                    field: 'action',
                    title: '操作',
                    halign: 'center',
                    align: 'left',
                    formatter: 'actionFormatter',
                    events: 'actionEvents',
                    width: 600,
                    clickToSelect: false
                }
            ]
        });
    });
    //参数传递
    function Search() {
        var temp = {
            datetime: $("#nowdate").val(),
        };
        return temp;
    }
    // 数据表格展开内容
    function detailFormatter(index, row) {
        var html = [];
        $.each(row, function (key, value) {
            html.push('<p><b>' + key + ':</b> ' + value + '</p>');
        });
        return html.join('');
    }

    function editAfter(obj, row) {
        $('#editForm')
        $(".chosen-select").trigger("chosen:updated");
    }

    function actionFormatter(value, row, index) {
        str = '\
            <button class="edit ml10 btn btn-primary">编辑</button>　\
            <button class="remove ml10 btn btn-primary">休眠户</button>　\
            <button class="add ml10 btn btn-primary">添加担保方</button>　\
            <button class="detail ml10 btn btn-primary" >详情</button>　\
            <button class="bestReport  ml10 btn btn-primary" data-toggle="tooltip" >上传尽调报告</button>　\
        ';
        if (row.status == 5) {
            str += '\
            <button class="storage ml10 btn btn-primary" data-toggle="tooltip" >存档</button>　\
            ';
        }
        if (row.status == 6) {
            str += '\
            <button class="cancelStorage ml10 btn btn-primary" data-toggle="tooltip" >取消存档</button>　\
            ';
        }
        return str;
    }

    window.actionEvents = {
        'click .edit': function (e, value, row, index) {
            $('#editModal').modal('show')
            $('#editForm').fillForm(row);

            editAfter('', row);
        },
        'click .remove': function (e, value, row, index) {
            $('#deleteModal').modal('show');
            $('.records').html(row.comName);
            var html = '';
            html += '<input type="hidden" name="id" value="' + row.id + '">';
            html += '<label class="control-label" for="comName">原因:</label>';
            html += '<textarea autofocus class="form-control" name="comName" minlength="5" maxlength="300" placeholder="输入原因" required>';
            $('#deleteForm').html(html);
        },
        'click .add': function (e, value, row, index) {
            $('#addModal').modal('show');
            //$('#allotForm').fillForm(row);
            $('#compay_id').val(row.id)
            /* $('.records').html(row.reallyName);
             var html = '';
             html += '<input type="hidden" name="id" value="' + row.id+ '">';
             $('#deleteForm').html(html);*/
        },
        'click .detail': function (e, value, row, index) {
            var id = row.id
            $.myAdmin.loadContent('#archives/detail?id=' + id + '');
        },
        'click .bestReport': function (e, value, row, index) {
            $('#bestReportModal').modal('show');
            $('#bestReport_compay_id').val(row.id)
        },
        'click .storage': function (e, value, row, index) {
            $('#storageModal').modal('show');
            $('.records').html(row.comName);
            var html = '';
            html += '<input type="hidden" name="id" value="' + row.id + '">';
            html += '<input type="hidden" name="operateType" value="7">';
            $('#storageForm').html(html);
        },
        'click .cancelStorage': function (e, value, row, index) {
            $('#cancelStorageModal').modal('show');
            $('.records').html(row.comName);
            var html = '';
            html += '<input type="hidden" name="id" value="' + row.id + '">';
            html += '<input type="hidden" name="operateType" value="8">';
            $('#cancelStorageForm').html(html);
        }
    };
    var setting = {
        view: {
            dblClickExpand: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        callback: {
            onClick: onClick
        }
    };

</script>